package ynu.edu.dish.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.dish.dto.DishCategoryDTO;
import ynu.edu.dish.service.DishCategoryService;
import ynu.edu.dish.vo.DishCategoryVO;

import java.util.List;

/**
 * 菜品分类控制器
 */
@RestController
@RequestMapping("/dish-categories")
@RequiredArgsConstructor
public class DishCategoryController {

    private final DishCategoryService dishCategoryService;

    /**
     * 创建菜品分类
     */
    @PostMapping
    public Result<DishCategoryVO> createDishCategory(@RequestBody @Valid DishCategoryDTO dishCategoryDTO, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 设置商家ID
        dishCategoryDTO.setMerchantId(merchantId);
        
        // 创建菜品分类
        DishCategoryVO dishCategoryVO = dishCategoryService.createDishCategory(dishCategoryDTO);
        return Result.success(dishCategoryVO);
    }

    /**
     * 更新菜品分类
     */
    @PutMapping("/{id}")
    public Result<DishCategoryVO> updateDishCategory(@PathVariable Long id, @RequestBody @Valid DishCategoryDTO dishCategoryDTO, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 设置ID和商家ID
        dishCategoryDTO.setId(id);
        dishCategoryDTO.setMerchantId(merchantId);
        
        // 更新菜品分类
        DishCategoryVO dishCategoryVO = dishCategoryService.updateDishCategory(dishCategoryDTO);
        return Result.success(dishCategoryVO);
    }

    /**
     * 获取菜品分类信息
     */
    @GetMapping("/{id}")
    public Result<DishCategoryVO> getDishCategoryById(@PathVariable Long id) {
        DishCategoryVO dishCategoryVO = dishCategoryService.getDishCategoryById(id);
        return Result.success(dishCategoryVO);
    }

    /**
     * 删除菜品分类
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDishCategory(@PathVariable Long id, HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 删除菜品分类
        boolean result = dishCategoryService.deleteDishCategory(id);
        return Result.success(result);
    }

    /**
     * 根据商家ID查询菜品分类列表（需要认证）
     */
    @GetMapping
    public Result<List<DishCategoryVO>> getDishCategoriesByMerchantId(HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 查询菜品分类列表
        List<DishCategoryVO> dishCategoryVOs = dishCategoryService.getDishCategoriesByMerchantId(merchantId);
        return Result.success(dishCategoryVOs);
    }

    /**
     * 根据商家ID查询菜品分类列表（公开接口）
     */
    @GetMapping("/merchant/{merchantId}")
    public Result<List<DishCategoryVO>> getDishCategoriesByMerchantIdPublic(@PathVariable Long merchantId) {
        // 查询菜品分类列表
        List<DishCategoryVO> dishCategoryVOs = dishCategoryService.getDishCategoriesByMerchantIdAndStatus(merchantId, 1);
        return Result.success(dishCategoryVOs);
    }

    /**
     * 根据商家ID分页查询菜品分类列表
     */
    @GetMapping("/list")
    public Result<Page<DishCategoryVO>> getDishCategoriesByMerchantIdPage(
            @RequestParam Long merchantId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        // 查询菜品分类分页列表
        Page<DishCategoryVO> dishCategoryPage = dishCategoryService.getDishCategoriesByMerchantId(merchantId, page, size);
        return Result.success(dishCategoryPage);
    }

    /**
     * 更新菜品分类状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateDishCategoryStatus(
            @PathVariable Long id,
            @RequestParam Integer status,
            HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 更新菜品分类状态
        boolean result = dishCategoryService.updateDishCategoryStatus(id, status);
        return Result.success(result);
    }
} 